﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Util;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using log4net;
using log4net.Layout;

namespace QunYing.Common
{
    public class Loger
    {
        private static log4net.ILog log;
        public static log4net.ILog Log
        {
            get
            {
                if (log == null)
                    log = Init();
                return log;
            }
        }

        private static ILog Init()
        {
            string LOG_PATTERN = "%d [%t] %-5p %c [%x] - %m%n";
            string LOG_FILE_PATH = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ruimei.log";
            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            hierarchy.Name = "myLog"; 
            TraceAppender tracer = new TraceAppender();
            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = LOG_PATTERN;
            patternLayout.ActivateOptions();
            tracer.Layout = patternLayout; tracer.ActivateOptions();
            hierarchy.Root.AddAppender(tracer);
            RollingFileAppender roller = new RollingFileAppender(); 
            roller.Layout = patternLayout; 
            roller.AppendToFile = true; 
            roller.RollingStyle = RollingFileAppender.RollingMode.Size; 
            roller.MaxSizeRollBackups = 4; 
            roller.MaximumFileSize = "100KB"; 
            roller.StaticLogFileName = true;
            roller.File = LOG_FILE_PATH; 
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);
            hierarchy.Root.Level = log4net.Core.Level.All;
            hierarchy.Configured = true;
          return  log4net.LogManager.GetLogger("myLog");
        }
    }
}
